package com.sunmouse.toolkit.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.StyleRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toollib.utils.anim.AnimUtil;

/**
 * Created by Administrator on 2017/9/21 0021.
 */
public class IpSettingDialog extends AlertDialog {
    /**
     * ip setting dialog
     */
    private EditText ipEt;
    private EditText portEt;
    private Button cancelBtn;
    private Button submitBtn;
    private View v;

    private static IpSettingDialog mDialog;

    protected IpSettingDialog(Context context) {
        this(context, 0);
    }

    protected IpSettingDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
//        v=
//        onAttachedToWindow();
    }

    /**
     * instance factory
     */
    public static IpSettingDialog getInstance(Activity act) {
        if (mDialog == null) {
            mDialog = new IpSettingDialog(act);
        }
        return mDialog;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtils.d("-----------------onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_setting_ip);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(AnimUtil.Transition.TITO);
        init();
    }

    @Override
    protected void onStart() {
        LogUtils.d("-----------------onStart");
        super.onStart();
    }

    @Override
    protected void onStop() {
        LogUtils.d("-----------------onStart");
        super.onStop();
    }

    private void init() {
        this.ipEt = (EditText) this.findViewById(R.id.ip_et);
        this.portEt = (EditText) this.findViewById(R.id.port_et);
        this.cancelBtn = (Button) this.findViewById(R.id.cancel_btn);
        this.submitBtn = (Button) this.findViewById(R.id.submit_btn);
    }

    /**
     * set button background
     */
    public void setCancelBackground(@DrawableRes int res) {
        if (res < 0) {
            throw new RuntimeException("res is invalid");
        }
        this.cancelBtn.setBackgroundResource(res);
    }

    public void setConfirmBackground(@DrawableRes int res) {
        if (res < 0) {
            throw new RuntimeException("res is invalid");
        }
        this.submitBtn.setBackgroundResource(res);
    }


    /**
     * show or hide
     */
    public void show() {
        if (!this.isShowing()) {
//            this.ipEt.requestFocus();
            this.show();
        }
    }

    public void dismiss() {
        if (this.isShowing()) {
            this.dismiss();
        }
    }


    /**
     * set text
     */
    public void setIp(String ip) {
        this.ipEt.setText(TextUtils.isEmpty(ip) ? "" : ip);
    }

    public void setPort(String port) {
        this.portEt.setText(TextUtils.isEmpty(port) ? "" : port);
    }

    /**
     * get text
     */
    public String getIp() {
        return this.ipEt.getText().toString();
    }

    public String getPort() {
        return this.portEt.getText().toString();
    }

    /**
     * set transition animation
     */
    public void setAnim(@StyleRes int anim) {
        if (anim <= 0) return;
        this.getWindow().setWindowAnimations(anim);
    }

    /**
     * set click event
     */
    public void setOnCancelClickListener(View.OnClickListener listener) {
        if (listener == null) {
            this.cancelBtn.setOnClickListener(mListener);//default handle method
            return;
        }
        this.cancelBtn.setOnClickListener(listener);
    }

    public void setOnSubmitClickListener(View.OnClickListener listener) {
        if (listener == null) {
            this.submitBtn.setOnClickListener(mListener);
            return;
        }
        this.submitBtn.setOnClickListener(listener);
    }


    /**
     * default click event
     */
    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };


}
